package jp.co.withone.osgi.gadget.pictureviewer.impl;

import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;
import jp.co.withone.osgi.gadget.pictureviewer.PictureController;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;

/**
 * The Activator class provide initialize and finalize this bundle.
 * 
 * @author funakky
 */
public class Activator implements BundleActivator, SettingViewListener {
	public static final ResourceBundle RB = ResourceBundle
			.getBundle("jp.co.withone.osgi.gadget.pictureviewer.resource.message");

	private final Object lock = new Object();

	private PictureViewerMainView viewer;
	private PictureViewerSettingView settingView;
	private Timer timer;
	private TimerTask task;
	private ServiceTracker tracker;
	private ServiceRegistration reg;

	private boolean stopped = true;

	private long interval = 10000L;

	/**
	 * Initialize this bundle.<br>
	 * This method has called by framework on this bundle start.
	 * 
	 * @param context
	 *            {@link BundleContext} object for this bundle
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 *      )
	 */
	public void start(BundleContext context) throws Exception {
		synchronized (this.lock) {

			this.stopped = false;

			this.viewer = new PictureViewerMainView();

			this.reg = context.registerService(SettingViewListener.class
					.getName(), this, null);

			this.tracker = new ServiceTracker(context, PictureController.class
					.getName(), null);
			this.tracker.open();

			this.timer = new Timer("PictureViewerTimer", true);
			this.task = new PictureControlTask(this.viewer, this.tracker);
			this.timer.schedule(this.task, 0, this.interval);
		}
	}

	/**
	 * Finalize this bundle.<br>
	 * This method has called by framework on this bundle stop.
	 * 
	 * @param context
	 *            {@link BundleContext} object for this bundle
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		synchronized (this.lock) {
			this.task.cancel();
			this.task = null;
			this.timer.cancel();
			this.timer = null;

			this.tracker.close();
			this.tracker = null;

			this.reg.unregister();
			this.reg = null;

			if (this.settingView != null) {
				this.settingView.dispose();
				this.settingView = null;
			}

			this.viewer.dispose();
			this.viewer = null;

			this.stopped = true;
		}
	}

	/**
	 * Set picture change interval.
	 * 
	 * @param interval
	 *            picture change interval
	 */
	public void setInterval(long interval) {
		synchronized (this.lock) {
			if (this.stopped) {
				return;
			}
			this.interval = interval;
			this.task.cancel();
			this.task = new PictureControlTask(this.viewer, this.tracker);
			this.timer.schedule(this.task, 0, this.interval);
		}
	}

	/**
	 * Get picture change interval.
	 * 
	 * @return picture change interval
	 */
	public long getInterval() {
		return this.interval;
	}

	/**
	 * Visible setting view.
	 * 
	 * @param x
	 *            view width position
	 * @param y
	 *            view height position
	 * @see jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener#show()
	 */
	@Override
	public void show(int x, int y) {
		synchronized (this.lock) {
			if (this.stopped) {
				return;
			}
			if (this.settingView == null) {
				this.settingView = new PictureViewerSettingView(this);
				this.settingView.setBounds(x, y, this.settingView.getWidth(),
						this.settingView.getHeight());
			}
			if (!this.settingView.isVisible()) {
				this.settingView.setVisible(true);
			}
		}
	}

}
